/**
 * @author xiaobyu
 * @created Apr 6, 2016
 * 
 */
package nconv.utils;

import nconv.convert.ConvertContext;
import nconv.convert.ConvertContext.DOC_TYPE;
import nconv.convert.ConvertUtils;
import nconv.wiki.MediaWikiProxy;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LogCenter {

	public static final String LOG_FILE_NAME = "output/log.txt";
	public static final String WIKI_LOG_TITLE = "Update log";

	private static boolean updateWikiLog = true;

	static BufferedWriter localLogWriter;

	public static enum DOC_OPERATION{
		CREATE, UPDATE, DELETE, NOTICE
	}

	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
	
	public static void log(String str) {
		logLocal(timeFormat.format(new Date()) + "|LOG|" + str);
	}

	public static final class WikiLogEntry {
		DOC_TYPE docType;
		String docName;
		String page;
		DOC_OPERATION oper;
		String time;
		String comments;
		public WikiLogEntry(String page, DOC_OPERATION oper, String comments) {
			this.docType = ConvertContext.getDocType();
			this.docName = ConvertContext.getDocName();
			this.page = page;
			this.oper = oper;
			this.time = timeFormat.format(new Date());
			this.comments = comments;
		}

		public WikiLogEntry(String page, DOC_OPERATION oper) {
			this(page, oper, null);
		}

		public DOC_TYPE getDocType() {
			return docType;
		}

		public String getDocName() {
			return docName;
		}

		public String getPage() {
			return page;
		}

		public DOC_OPERATION getDocOperation() {
			return oper;
		}

		public String getTime() {
			return time;
		}

		public String getComments() {
			return comments;
		}
	}

	private static List<WikiLogEntry> logEntries = new ArrayList<>();

	@Deprecated
	public static void logWiki(WikiLogEntry entry) {
		logEntries.add(entry);
	}

	public static void logWiki(String page, DOC_OPERATION oper, String comments) {
		logEntries.add(new WikiLogEntry(page, oper, comments));
		StringBuffer sb = new StringBuffer(timeFormat.format(new Date()));
		sb.append("|");
		if (oper.equals(DOC_OPERATION.NOTICE)) {
			sb.append("<font color=red>" + oper + "</font>");
		} else {
			sb.append(oper);
		}
		if (ConvertContext.getDocType() != null) {
			sb.append("|Doc Type: " + ConvertContext.getDocType());
		}
		if (ConvertContext.getDocName() != null) {
			sb.append("|Doc Name: " + ConvertContext.getDocName());
		}
		if (page != null) {
			sb.append("|Page Name: " + page);
		}
		if (comments != null) {
			sb.append("|" + comments);
		}
		logLocal(sb.toString());
	}

	private static void logLocal(String text) {
		System.out.println(text);
		try {
			if (localLogWriter == null) {
				localLogWriter = new BufferedWriter(new FileWriter(LOG_FILE_NAME));
			}
			localLogWriter.write(text + "\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void logWiki(String page, DOC_OPERATION oper) {
		logWiki(page, oper, null);
	}

	public static void updateLogs() {
		if (localLogWriter != null) {
			try {
				localLogWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		if (logEntries.size() == 0) return;

		if (!ConvertUtils.uploadDocWikiPage && !ConvertUtils.uploadImage
				&& !ConvertUtils.uploadAVPWikiPage && !ConvertUtils.uploadDiameterCommandPage) {
			return;
		}

		StringBuilder sb = new StringBuilder("{|class=\"wikitable collapsible\"\n!Doc Type\n!Doc Name\n!Page Title\n!Operation\n!Time\n!Comments");
		for (WikiLogEntry entry : logEntries) {
			sb.append("\n|-\n")
					.append("|" + entry.getDocType() + "\n")
					.append("|" + entry.getDocName() + "\n")
					.append("|" + (entry.getPage() == null ? "N/A" : "[[" + entry.getPage() + "]]") + "\n")
					.append("|" + entry.getDocOperation() + "\n")
					.append("|" + entry.getTime() + "\n")
					.append("|" + (entry.comments == null ? "" : entry.comments));
		}
		sb.append("\n|}");
		if (updateWikiLog) {
			String dateHeader = formatDateHeader();
			MediaWikiProxy proxy = MediaWikiProxy.getWikiSite(MediaWikiProxy.WikiSite.AUTO_WIKI);
			try {
				String content = proxy.getPageContent(WIKI_LOG_TITLE);
				if (content == null) {
					content = dateHeader + "\n\n" + sb.toString();
				} else {
					if (content.indexOf(dateHeader) < 0) {
						content += "\n" + dateHeader;
					}
					content += "\n" + sb.toString();
				}
				proxy.createPage(WIKI_LOG_TITLE, content, "add wiki update log");
			} catch (Exception e) {
				System.err.println("Error while updating wiki log: " + e.getLocalizedMessage());
			}
		}
	}

	public static String formatDateHeader() {
		return "==" + dateFormat.format(new Date()) + "==";
	}

}
